package indi.kider.server.bill.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import indi.kider.common.model.apartment.Houses;
import indi.kider.common.model.bill.MeterAddress;
import indi.kider.server.bill.service.MeterApiService;
import indi.kider.server.bill.vo.MeterAddressQueryModel;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;

import java.util.Date;
import java.util.List;

/**
 * 所以和设备账单相关的接口
 */
@RestController
@RequestMapping("meterApi")
@Log4j
public class ApiController {

    @Autowired
    private MeterApiService meterApiService;

    /**
     * @api {PUT} bill
     * @apiGroup balance-bill
     * @apiDescription 后台定时结算（内部服务）
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meterApi/queryMeterAddressByPowerMeterCode/{meterCode}/{meterType}
     * {}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"请求成功！","data":[{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}]}
     */
    @GetMapping("/queryMeterAddressByPowerMeterCode/{meterCode}/{meterType}")
    public CommonResponse<List<MeterAddress>> queryMeterAddressByPowerMeterCode(@PathVariable String meterCode, @PathVariable Integer meterType) {
        return CommonResponse.buildGetSuccessResponse(meterApiService.queryMeterAddressByMeterCode(meterCode, meterType, new Date()));
    }


    /**
     * @api {PUT} bill
     * @apiGroup balance-bill
     * @apiDescription 后台定时结算（内部服务）
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meterApi/queryMeterAddressByPowerMeterCodes/{jsonString}
     * {}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"请求成功！","data":[{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}]}
     */
    @GetMapping("/queryMeterAddressByPowerMeterCodes/{jsonString}")
    public CommonResponse<List<MeterAddress>> queryMeterAddressByFloor(@PathVariable String jsonString) {
        JSONObject json = JSONObject.parseObject(jsonString);
        log.info("【" + json + "】");
        MeterAddressQueryModel model = JSON.toJavaObject(json, MeterAddressQueryModel.class);

        return CommonResponse.buildGetSuccessResponse(meterApiService.queryMeterAddressByFloor(model.getMeters(), model.getMeterType(), new Date()));
    }

    /**
     * @api {PUT} bill
     * @apiGroup balance-bill
     * @apiDescription 后台定时结算（内部服务）
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meterApi/queryMeterAddressByCommunityId/{communityCode}
     * {}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"请求成功！","data":[{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}]}
     */
    @GetMapping("/queryMeterAddressByCommunityId/{communityCode}")
    public CommonResponse<List<MeterAddress>> queryMeterAddressByCommunityId(@PathVariable String communityCode) {
        return CommonResponse.buildGetSuccessResponse(meterApiService.queryMeterAddressByCommunityId(communityCode, new Date()));
    }

    /**
     * @api {PUT} bill
     * @apiGroup balance-bill
     * @apiDescription 后台定时结算（内部服务）
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meterApi/queryPowerMeterAndWaterMeterByHousesId/{housesId}
     * {}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"请求成功！","data":[{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}]}
     */
    @GetMapping("/queryPowerMeterAndWaterMeterByHousesId/{housesId}")
    public CommonResponse<Houses> queryPowerMeterAndWaterMeterByHousesId(@PathVariable Long housesId) {
        return CommonResponse.buildGetSuccessResponse(meterApiService.queryPowerMeterAndWaterMeterByHousesId(housesId));
    }

    /**
     * @api {PUT} bill
     * @apiGroup balance-bill
     * @apiDescription 后台定时结算（内部服务）
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meterApi/queryPowerMeterAndWaterMeterByHousesId/{housesId}
     * {}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"请求成功！","data":[{"id":1,"billType":1,"payState":0,"billTime":"2018-05-10 08:58:53","payTime":null,"billAttribution":1,"lastRead":"22","lastReadTime":"2018-05-10 08:59:02","thisRead":"22","thisReadTime":"2018-05-10 08:59:05","absoluteNumber":"121","price":"1","total":"1","paySerial":""}]}
     */
    @GetMapping("/queryPowerMeterAndWaterMeterByTenantId/{tenantId}")
    public CommonResponse<Houses> queryPowerMeterAndWaterMeterByTenantId(@PathVariable Long tenantId) {
        return CommonResponse.buildGetSuccessResponse(meterApiService.queryPowerMeterAndWaterMeterByTenantId(tenantId));
    }

    /**
     * 根据租客的ID查询租客姓名
     *
     * @param tenantIds
     * @return
     */
    @PostMapping("/queryTenantById")
    public CommonResponse queryTenantById(@RequestBody List<Long> tenantIds){
        if (tenantIds == null || tenantIds.isEmpty()){
            return CommonResponse.buildBadRequestResponse("id不能为空");
        }else {
            return CommonResponse.buildGetSuccessResponse(meterApiService.queryTenantByIds(tenantIds));

        }
    }

    /**
     * 根据房间的ID查询房间信息
     *
     * @param housesIds
     * @return
     */
    @PostMapping("/queryHousesById")
    public CommonResponse queryHousesById(@RequestBody List<Long> housesIds){
        if (housesIds == null || housesIds.isEmpty()){
            return CommonResponse.buildBadRequestResponse("id不能为空");
        }else {
            return CommonResponse.buildGetSuccessResponse(meterApiService.queryHousesByIds(housesIds));
        }
    }
}
